package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Iterator;

import messages.ErrorMessage;
import messages.Message;
import messages.MessageId;




public class ServerListener implements ActionListener {
    
    public Client client;


    public ServerListener(Client client) {
        this.client=client;
    }


    @Override
    public void actionPerformed(ActionEvent arg0) {
        try {
            while(client.bufferedReader.ready()) {
                Message message= MessageId.parseMessage(client.bufferedReader); //blocking
                if(message!=null) {
                    System.out.println("Received message: "+message.writeMessage());
                    if(message.getType().equals("200")) 
                    {
                        System.out.println("OK");
                        if(message.getDestinationAddress()!=null) {
                            client.listDialog.label.setText(message.writeMessage());
                            client.callDialog.setVisible(false);
                            
                            InetAddress adresseClientP2P=InetAddress.getByName(message.getDestinationAddress());
                            
                            client.peerListener.connectToOtherClient(adresseClientP2P, Integer.parseInt(message.getDestinationPort()));
                            
                        }
                    }
                    else if(message.getType().equals("CLIENTS")) {
                        
                        client.listDialog.clientListData.removeAllElements();
                         for (Iterator iterator = message.getClientList().iterator(); iterator.hasNext();) {
                          String aclient = (String) iterator.next();
                          client.listDialog.clientListData.addElement(aclient);
                         }
                    }
                    else if(message.getType().equals("401")) {
                        System.out.println(message.toString());
                    }
                    else if(message.getType().equals("100")) {
                        client.listDialog.label.setText(message.writeMessage());
                    }
                    else if(message.getType().equals("101")) {
                        client.listDialog.label.setText(message.writeMessage());
                    }
                    else if(message.getType().equals("180")) {
                        client.listDialog.label.setText(message.writeMessage());
                    }
                    else if(message.getType().equals("404")) {
                        client.listDialog.label.setText(message.writeMessage());
                        client.listDialog.btnCall.setEnabled(true);
                    }
                    else if(message.getType().equals("603")) {
                        client.listDialog.label.setText(message.writeMessage());
                        client.listDialog.btnCall.setEnabled(true);
                    }
                    else if(message.getType().equals("INVITE")) {
                        if(client.busy) {
                            ErrorMessage messError=new ErrorMessage("603");
                            
                            try {
                                client.bufferedWriter.write(messError.writeMessage());
                                client.bufferedWriter.flush();
                              } catch (IOException e1) {
                                    e1.printStackTrace();
                              }
                        }
                        else {
                            client.callDialog.label.setText(message.getUserName());
                            client.callDialog.setLocationRelativeTo(client.listDialog);
                            client.callDialog.setVisible(true);
                        }
                    }
                    
                }

   }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
